package com.dev;
import android.os.Environment;
import com.log;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
public class TabData {
	MainActivity ma;
	File dataSrc;
    TabData(MainActivity ma){
		this.ma=ma;
		dataSrc=new File(Environment.getExternalStorageDirectory(),ma.getPackageName()+".data.txt");
		if(!dataSrc.exists()){
			try{
				dataSrc.createNewFile();
			}catch(Exception e){
				log.alert(e.toString());
			}
		}
	}
	TabList loadTabList(){
		TabList tl=new TabList();
		try{
			FileReader fr=new FileReader(dataSrc);
			StringBuilder sb=new StringBuilder();
			outmost:
			for(int i=fr.read();true;i=fr.read()){
				if(i>=0){
					char ch=(char)i;
					if(ch=='\n'){
						String s=sb.toString();
						sb=new StringBuilder();
						if(s.equals(FileBrowserInfo.tabKind)){
							FileBrowserInfo fbi=new FileBrowserInfo();
							for(i=fr.read();true;i=fr.read()){
								if(i>=0){
									ch=(char)i;
									if(ch=='\n'){
										sb=new StringBuilder();
										break;
									}else if(ch=='='){
										for(i=fr.read();true;i=fr.read()){
											if(i>=0){
												ch=(char)i;
												if(ch=='\n'){
													s=sb.toString();
													File f=new File(s);
													fbi.home=f;
													break;
												}
												sb.append(ch);
											}else{
												break outmost;
											}
										}
										break;
									}
								}else{
									break outmost;
								}
							}
							for(i=fr.read();true;i=fr.read()){
								if(i>=0){
									ch=(char)i;
									if(ch=='\n'){
										sb=new StringBuilder();
										break;
									}else if(ch=='='){
										for(i=fr.read();true;i=fr.read()){
											if(i>=0){
												ch=(char)i;
												if(ch=='\n'){
													s=sb.toString();
													sb=new StringBuilder();
													File f=new File(s);
													if(f.exists()){
														fbi.currentDir=f;
													}else{
														fbi.currentDir=fbi.home;
													}
													break;
												}
												sb.append(ch);
											}else{
												break outmost;
											}
										}
										break;
									}
								}else{
									break outmost;
								}
							}
							tl.addTabInfo(fbi);
						}else if(s.equals(HtmlBrowserInfo.tabKind)){
						}else if(s.equals(MediaAudioBrowser.tabKind)){
							MediaAudioBrowser mab=new MediaAudioBrowser();
							tl.addTabInfo(mab);
						}else if(s.equals(AppListInfo.tabKind)){
							AppListInfo ali=new AppListInfo();
							tl.addTabInfo(ali);
						}else if(s.equals(JavaEditorInfo.tabKind)){
							JavaEditorInfo jei=new JavaEditorInfo();
							for(i=fr.read();true;i=fr.read()){
								if(i>=0){
									ch=(char)i;
									if(ch=='\n'){
										s=sb.toString();
										sb=new StringBuilder();
										File f=new File(s);
										jei.src=f;
										tl.addTabInfo(jei);
										break;
									}else{
										sb.append(ch);
									}
								}else{
									s=sb.toString();
									File f=new File(s);
									if(f.exists()){
										jei.src=f;
										tl.addTabInfo(jei);
									}
									break outmost;
								}
							}
						}else if(s.equals(ArchiveViewer.tabKind)){
							ArchiveViewer av=new ArchiveViewer();
							for(i=fr.read();true;i=fr.read()){
								if(i>=0){
									ch=(char)i;
									if(ch=='\n'){
										s=sb.toString();
										sb=new StringBuilder();
										File f=new File(s);
										av.src=f;
										tl.addTabInfo(av);
										break;
									}else{
										sb.append(ch);
									}
								}else{
									s=sb.toString();
									File f=new File(s);
									if(f.exists()){
										av.src=f;
										tl.addTabInfo(av);
									}
									break outmost;
								}
							}
						}else if(s.equals(TextEditor.tabKind)){
							TextEditor te=new TextEditor();
							for(i=fr.read();true;i=fr.read()){
								if(i>=0){
									ch=(char)i;
									if(ch=='\n'){
										s=sb.toString();
										sb=new StringBuilder();
										File f=new File(s);
										te.src=f;
										tl.addTabInfo(te);
										break;
									}else{
										sb.append(ch);
									}
								}else{
									s=sb.toString();
									File f=new File(s);
									if(f.exists()){
										te.src=f;
										tl.addTabInfo(te);
									}
									break outmost;
								}
							}
						}else if(s.equals(ImageManipulation.tabKind)){
							for(i=fr.read();;i=fr.read()){
								if(i>=0){
									ch=(char)i;
									if(ch=='\n'){
										s=sb.toString();
										sb=new StringBuilder();
										File f=new File(s);
										if(f.exists()){
											ImageManipulation im=new ImageManipulation();
											im.src=f;
											tl.addTabInfo(im);
										}
										break;
									}else{
										sb.append(ch);
									}
								}else{
									s=sb.toString();
									File f=new File(s);
									if(f.exists()){
										ImageManipulation im=new ImageManipulation();
										im.src=f;
										tl.addTabInfo(im);
									}
									break outmost;
								}
							}
						}else if(s.equals(CEditor.tabKind)){
							for(i=fr.read();;i=fr.read()){
								if(i>=0){
									ch=(char)i;
									if(ch=='\n'){
										s=sb.toString();
										sb.setLength(0);
										File f=new File(s);
										if(f.exists()){
											CEditor ce=new CEditor();
											ce.src=f;
											tl.addTabInfo(ce);
										}
										break;
									}else{
										sb.append(ch);
									}
								}else{
									s=sb.toString();
									File f=new File(s);
									if(f.exists()){
										CEditor ce=new CEditor();
										ce.src=f;
										tl.addTabInfo(ce);
									}
									break outmost;
								}
							}
						}else{
							log.alert("Unexpected tab type:",s);
							break outmost;
						}
					}else{
						sb.append(ch);
					}
				}else{
					break;
				}
			}
		}catch(Exception e){
			log.alert(e.toString());
		}
		return tl;
	}
	public void updateSavedTabList(TabList tl){
		try{
			FileWriter fw=new FileWriter(dataSrc,false);
			for(int index=0,len=tl.ts.size();index<len;index++){
				Object child=tl.ts.get(index);
				if(child instanceof FileBrowserInfo){
					FileBrowserInfo fbi=(FileBrowserInfo)child;
					fw.write(fbi.tabKind+'\n');
					fw.write("home="+fbi.home.getAbsolutePath()+'\n');
					fw.write("currentDir="+fbi.currentDir.getAbsolutePath()+'\n');
				}else if(child instanceof HtmlBrowserInfo){
					HtmlBrowserInfo hbi=(HtmlBrowserInfo)child;
					fw.write(HtmlBrowserInfo.tabKind+'\n');
					fw.write("src="+hbi.src.getAbsolutePath()+'\n');
				}else if(child instanceof MediaAudioBrowser){
					fw.write(MediaAudioBrowser.tabKind+'\n');
				}else if(child instanceof JavaEditorInfo){
					JavaEditorInfo jei=(JavaEditorInfo)child;
					fw.write(JavaEditorInfo.tabKind+'\n');
					fw.write(jei.src.getAbsolutePath()+'\n');
				}else if(child instanceof AppListInfo){
					fw.write(AppListInfo.tabKind+'\n');
				}else if(child instanceof ArchiveViewer){
					ArchiveViewer av=(ArchiveViewer)child;
					fw.write(ArchiveViewer.tabKind+'\n');
					fw.write(av.src.getAbsolutePath()+'\n');
				}else if(child instanceof TextEditor){
					TextEditor te=(TextEditor)child;
					fw.write(TextEditor.tabKind+'\n');
					fw.write(te.src.getAbsolutePath()+'\n');
				}else if(child instanceof ImageManipulation){
					ImageManipulation im=(ImageManipulation)child;
					fw.write(ImageManipulation.tabKind+'\n');
					fw.write(im.src.getAbsolutePath()+'\n');
				}else if(child instanceof CEditor){
					CEditor ce=(CEditor)child;
					fw.write(ce.tabKind+'\n');
					fw.write(ce.src.getAbsolutePath()+'\n');
				}
			}
			fw.close();
		}catch(Exception e){
			log.alert(e.toString());
		}
	}
	public static class TabList{
		ArrayList<Object> ts=new ArrayList<Object>();
		public void addTabInfo(Object tabInfo){
			if(tabInfo instanceof FileBrowserInfo){
			}else if(isExists(tabInfo)>=0){
				throw new IllegalArgumentException("TabInfo is existed");
			}
			ts.add(tabInfo);
		}
		int remove(Object tabInfo){//if not found,return -1
			Class tabInfo_class=tabInfo.getClass();
			for(int index=0,len=ts.size();true;index++){
				if(index<len){
					Object ob=ts.get(index);
					if(ob.getClass()==tabInfo_class){
						if(ob instanceof FileBrowserInfo){
							FileBrowserInfo tabInfo_=(FileBrowserInfo)tabInfo;
							FileBrowserInfo fbi=(FileBrowserInfo)ob;
							if(fbi.home.equals(tabInfo_.home)){
								ts.remove(index);
								return index;
							}
						}else if(ob instanceof HtmlBrowserInfo){
							HtmlBrowserInfo tabInfo_=(HtmlBrowserInfo)tabInfo;
							HtmlBrowserInfo hbi=(HtmlBrowserInfo)ob;
							if(hbi.src.equals(tabInfo_.src)){
								ts.remove(index);
								return index;
							}
						}else if(ob instanceof MediaAudioBrowser){
							ts.remove(index);
							return index;
						}else if(ob instanceof JavaEditorInfo){
							JavaEditorInfo ob_=(JavaEditorInfo)ob;
							JavaEditorInfo tabInfo_=(JavaEditorInfo)tabInfo;
							if(ob_.src.equals(tabInfo_.src)){
								ts.remove(index);
								return index;
							}
						}else if(ob instanceof ArchiveViewer){
							ArchiveViewer av=(ArchiveViewer)ob;
							ArchiveViewer tabInfo_=(ArchiveViewer)tabInfo;
							if(av.src.equals(tabInfo_.src)){
								ts.remove(index);
								return index;
							}
						}
					}
				}else{
					break;
				}
			}
			return -1;
		}
		int isExists(Object tabInfo){//if not exists then return -1
			Class tabInfo_class=tabInfo.getClass();
			for(int index=0,len=ts.size();true;index++){
				if(index<len){
					Object ob=ts.get(index);
					if(ob.getClass()==tabInfo_class){
						if(ob instanceof FileBrowserInfo){
							FileBrowserInfo tabInfo_=(FileBrowserInfo)tabInfo;
							FileBrowserInfo fbi=(FileBrowserInfo)ob;
							if(fbi.home.equals(tabInfo_.home)){
								return index;
							}
						}else if(ob instanceof HtmlBrowserInfo){
							HtmlBrowserInfo tabInfo_=(HtmlBrowserInfo)tabInfo;
							HtmlBrowserInfo hbi=(HtmlBrowserInfo)ob;
							if(hbi.src.equals(tabInfo_.src)){
								return index;
							}
						}else if(ob instanceof MediaAudioBrowser){
							return index;
						}else if(ob instanceof JavaEditorInfo){
							JavaEditorInfo ob_=(JavaEditorInfo)ob;
							JavaEditorInfo tabInfo_=(JavaEditorInfo)tabInfo;
							if(ob_.src.equals(tabInfo_.src)){
								return index;
							}
						}else if(ob instanceof AppListInfo){
							return index;
						}else if(ob instanceof ArchiveViewer){
							ArchiveViewer av=(ArchiveViewer)ob;
							ArchiveViewer tabInfo_=(ArchiveViewer)tabInfo;
							if(av.src.equals(tabInfo_.src)){
								return index;
							}
						}else if(ob instanceof TextEditor){
							TextEditor te=(TextEditor)ob;
							if(ob.equals(tabInfo)){
								return index;
							}
						}
					}
				}else{
					break;
				}
			}
			return -1;
		}
	}
	public static class FileBrowserInfo {
		final static String tabKind="fileBrowser";
		public File home;
		public File currentDir;
		public int sort;
		public boolean fileFirst;
	}
	public static class MediaAudioBrowser {
		final static String tabKind="mediaAudioBrowser";
		final static String tabTitle="MediaAudio";
	}
	public static class HtmlBrowserInfo {
		final static String tabKind="htmlBrowser";
		File src;
	}
	public static class TextBrowserInfo {
		final static String tabKind="textBrowser";
		boolean wrap;
		File src;
	}
	public static class JavaEditorInfo {
		final static String tabKind="javaEditor";
		public File src;
	}
	public static class AppListInfo{
		final static String tabKind="appListInfo";
		final static String tabTitle="应用列表";
	}
	public static class MediaImageInfo{
		final static String tabKind="MediaImageInfo";
		final static String tabTitle="媒体图像";
	}
	public static class ArchiveViewer{
		public File src;
		final static String tabKind="archiveViewer";
	}
	public static class XmlEditor{
		File src;
		final static String tabKind="xmlEditor";
	}
	public static class ImageManipulation{
		public File src;
		public final static String tabKind="imageManipulation";
	}
	public static class CEditor{
		public File src;
		final static String tabKind="cEditor";
	}
	public static class TextEditor{
		public File src;
		final static String tabKind="textEditor";
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof TextEditor){
				TextEditor te=(TextEditor)obj;
				if(te.src!=null){
					if(te.src.equals(src))return true;
				}
			}
			return false;
		}
	}
}
